//
//  OutlineViewController.m
//  NVAReader
//
//  Created by DuyLK on 6/7/14.
//  Copyright (c) 2014 com.nva.nvareader. All rights reserved.
//

#import "OutlineViewController.h"

@implementation OutlineViewController
-(id) init
{
    self = [super init];
    if (self) {
        _root = [[NSMutableArray alloc] init];
        OutlineItem *item = [[OutlineItem alloc] initWithTitle:@"Chap 1" page:@"1"];
        [item addChildrenItem:[[OutlineItem alloc] initWithTitle:@"Chap 2" page:@"2"]];
        [item addChildrenItem:[[OutlineItem alloc] initWithTitle:@"Chap 3" page:@"3"]];
        [item addChildrenItem:[[OutlineItem alloc] initWithTitle:@"Chap 4" page:@"4"]];
        
        [(OutlineItem*)[item.childrenItem objectAtIndex:0] addChildrenItem:[[OutlineItem alloc] initWithTitle:@"Chap 1.1" page:@"4"]];
        
        [_root addObject:item];
    }
    return  self;
}
-(id) initWithData:(NSMutableArray *)data
{
    self = [super init];
    _root = [[NSMutableArray alloc] init];
    _root = data;
    return self;
}
#pragma nsOutline
-(NSInteger) outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
    return  !item ? [self.root count] : [[item childrenItem] count];
}

-(BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
    return  !item ? YES : [[item childrenItem] count] != 0;
}

-(id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
    return  !item ? [self.root objectAtIndex:index] : [[item childrenItem] objectAtIndex:index];
}

-(id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
    if ([[tableColumn identifier] isEqualToString:@"title"]) {
        return  [item title];
    }else if([[tableColumn identifier] isEqualToString:@"page"])
    {
        return [item page];
    }
    return @"no here!";
}
-(int) countChild
{
    return (int)[_root count];
}
@end
